package com.demo.api;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;

import java.lang.ref.WeakReference;

public class TestApiManager {


    private TestApiManager() {
    }

    private static class TestApiManagerHolder {

        private static TestApiManager manager = new TestApiManager();
    }


    public static TestApiManager getInstance() {
        return TestApiManagerHolder.manager;
    }

    /**
     * 获取activityInfo
     * @param context
     * @return
     */
    public ActivityInfo getActivityInfo(WeakReference<Activity> context) {

        Activity activity = null;
        ActivityInfo info = null;
        activity = context.get();
        if (activity == null) {
            return null;
        }
        try {
            info = activity.getPackageManager().getActivityInfo(activity.getComponentName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

        if (info != null) {
            return info;
        }
        return null;
    }

    /**
     * 获取ApplicationInfo
     * @param context
     * @return
     */
    public ApplicationInfo getApplicationInfo(WeakReference<Context> context) {

        ApplicationInfo info = null;
        Context context1=context.get();
        try {
            info = context1.getPackageManager().getApplicationInfo(context1.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (info != null) {
            return info;
        }
        return null;
    }
}
